# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: fengyang, donggu

import requests
from decimal import *
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def generate():
  mea = 'Spot.Gateio.v4'
  me = 'Spot.Gateio'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get("https://api.gateio.ws/api/v4/spot/currency_pairs").json()

  pis = []
  for info in data:
    if info["trade_status"] != "tradable":
      continue

    native_symbol = info['id']
    native_base = info['base']
    native_quote = info['quote']
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)

    min_amount_exist = False
    if "min_quote_amount" in info:
      min_amount = float(info["min_quote_amount"])
      min_amount_exist = True

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=float(Decimal("0.1")**Decimal(info['precision'])),
        qty_ticksize=float(Decimal("0.1")**Decimal(info['amount_precision']))
    )
    if min_amount_exist :
      pi["min_amount"] = min_amount

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  symbol_list = [info['native_symbol'] for info in data]
  quote_currency_list = list({info['native_quote'] for info in data})
  base_currency_list = list({info['native_base'] for info in data})
  currency_list = base_currency_list + quote_currency_list
  return {
      'symbol_list': symbol_list,
      'currency_list': currency_list,
      'quote_currency_list': quote_currency_list,
  }


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
